package com.etc.delivery.controller.admin;

import com.etc.delivery.dto.DriverDto;
import com.etc.delivery.entity.Waybill;
import com.etc.delivery.service.admin.IAdminDriverService;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RequestMapping(value = "/admin")
@Controller
public class AdminDriverController {

    @Resource
    private IAdminDriverService driverService;

    @RequestMapping(value = "/queryDriver")
    @ResponseBody
    public PageInfo<DriverDto> queryDriver(String name, Integer warehouseId, Integer pageNum, Integer pageSize) {
        if (warehouseId == null) {
            warehouseId = 0;
        }


        return driverService.queryDriver(name, warehouseId, pageNum, pageSize);
    }

    @RequestMapping(value = "deleteDriver")
    @ResponseBody
    public Map<String, String> deleteWarehouse(Integer id) {
        Map<String, String> map = new HashMap<>();
        List<Waybill> list = driverService.queryDriverWaybill(id);
        if (!list.isEmpty()) {
            map.put("code", "0");
            map.put("msg", "司机有运单未完成，删除失败");
            return map;
        }


        boolean flag = driverService.deleteDriver(id);
        if (flag) {
            map.put("code", "1");
            map.put("msg", "删除成功");
        } else {
            map.put("code", "0");
            map.put("msg", "删除失败");
        }
        return map;
    }


}
